package com.yuyou.fn.tms.controller.resource;

import com.alibaba.fastjson.JSONArray;
import com.yuyou.fn.tms.controller.Binder;
import com.yuyou.fn.tms.service.IResourceSyncService;
import com.yuyou.fn.tms.util.Signs;
import com.yuyou.fn.tms.vo.PageQueryResult;
import com.yuyou.fn.tms.vo.ResponseResult;
import com.yuyou.fn.tms.vo.resource.*;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@RequestMapping("/api/resource")
public class OpenResourceController extends Binder {

    private final static Logger log  = LoggerFactory.getLogger(OpenResourceController.class);

    @Value("${resource.sign.key}")
    private String signKey ;



    @Resource
    private IResourceSyncService resourceSyncService;

    @ApiOperation(value = "同步管理区域" )
    @RequestMapping( value = "/syncAreaForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<List<ResourceDistrict>> syncAreaForZyRes(@Valid @ModelAttribute ResourceRequestParam requestParam){
        Signs.validResourceSign(requestParam.getTimeStamp(),null,requestParam.getSign(),requestParam.getLastModifyTime(),signKey);
        List<ResourceDistrict> resourceDistricts = resourceSyncService.syncAreaForZyRes(requestParam.getLastModifyTime());
        return ResponseResult.success(resourceDistricts) ;
    }

    @ApiOperation(value = "同步部门" )
    @RequestMapping( value = "/syncDepartmentForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<List<ResourceCampus>> syncDepartmentForZyRes(@Valid @ModelAttribute ResourceRequestParam requestParam){
        Signs.validResourceSign(requestParam.getTimeStamp(),null,requestParam.getSign(),requestParam.getLastModifyTime(),signKey);
        List<ResourceCampus> resourceCampuses = resourceSyncService.syncDepartmentForZyRes(requestParam.getLastModifyTime());
        return ResponseResult.success(resourceCampuses) ;
    }

    @ApiOperation(value = "同步城市" )
    @RequestMapping( value = "/syncCityForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<List<ResourceCity>> syncCityForZyRes(@Valid @ModelAttribute ResourceRequestParam requestParam){
        Signs.validResourceSign(requestParam.getTimeStamp(),null,requestParam.getSign(),requestParam.getLastModifyTime(),signKey);
        List<ResourceCity> resourceCities = resourceSyncService.syncCityForZyRes(requestParam.getLastModifyTime());
        return ResponseResult.success(resourceCities) ;
    }

    @ApiOperation(value = "同步年级" )
    @RequestMapping( value = "/syncGradeForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<List<ResourceGrade>> syncGradeForZyRes(@Valid @ModelAttribute ResourceRequestParam requestParam){
        Signs.validResourceSign(requestParam.getTimeStamp(),null,requestParam.getSign(),requestParam.getLastModifyTime(),signKey);
        List<ResourceGrade> resourceGrades = resourceSyncService.syncGradeForZyRes(requestParam.getLastModifyTime());
        return ResponseResult.success(resourceGrades) ;
    }

    @ApiOperation(value = "同步科目" )
    @RequestMapping( value = "/syncSubtForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<List<ResourceSubject>> syncSubtForZyRes(@Valid @ModelAttribute ResourceRequestParam requestParam){
        Signs.validResourceSign(requestParam.getTimeStamp(),null,requestParam.getSign(),requestParam.getLastModifyTime(),signKey);
        List<ResourceSubject> resourceSubjects = resourceSyncService.syncSubtForZyRes(requestParam.getLastModifyTime());
        return ResponseResult.success(resourceSubjects) ;
    }

    @ApiOperation(value = "同步学生信息" )
    @RequestMapping( value = "/syncStudentForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<PageQueryResult<ResourceStudent>> syncStudentForZyRes(@Valid @ModelAttribute PageRequestParam pageRequestParam){
        Signs.validResourceSign(pageRequestParam.getTimeStamp(),null,pageRequestParam.getSign(),pageRequestParam.getLastModifyTime(),signKey);
        PageQueryResult<ResourceStudent> resourceStudents = resourceSyncService.syncStudentForZyRes(pageRequestParam);
        return ResponseResult.success(resourceStudents) ;
    }


    @ApiOperation(value = "同步报名信息" )
    @RequestMapping( value = "/syncRegistForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<PageQueryResult<ResourceReg>> syncRegistForZyRes(@Valid @ModelAttribute PageRequestParam pageRequestParam){
        Signs.validResourceSign(pageRequestParam.getTimeStamp(),null,pageRequestParam.getSign(),pageRequestParam.getLastModifyTime(),signKey);
        PageQueryResult<ResourceReg> resourceRegs = resourceSyncService.syncRegistForZyRes(pageRequestParam);
        return ResponseResult.success(resourceRegs) ;
    }


    @ApiOperation(value = "同步导入记录" )
    @RequestMapping( value = "/syncImportLogForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<PageQueryResult<ResourceCustomer>> syncImportLogForZyRes(@Valid @ModelAttribute PageRequestParam pageRequestParam){
        Signs.validResourceSign(pageRequestParam.getTimeStamp(),null,pageRequestParam.getSign(),pageRequestParam.getLastModifyTime(),signKey);
        PageQueryResult<ResourceCustomer> resourceCustomers = resourceSyncService.syncImportLogForZyRes(pageRequestParam);
        return ResponseResult.success(resourceCustomers) ;
    }


    @ApiOperation(value = "同步班型" )
    @RequestMapping( value = "/syncClvForZyRes" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<List<ResourceLevel>> syncClvForZyRes(@Valid @ModelAttribute ResourceRequestParam requestParam){
        Signs.validResourceSign(requestParam.getTimeStamp(),null,requestParam.getSign(),requestParam.getLastModifyTime(),signKey);
        List<ResourceLevel> resourceLevels = resourceSyncService.syncClvForZyRes(requestParam.getLastModifyTime());
        return ResponseResult.success(resourceLevels) ;
    }

    @ApiOperation(value = "资源回写接口" )
    @RequestMapping( value = "/receiveResFromRmp" , method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResponseResult<String> receiveResFromRmp( @RequestBody ReceiveVo receiveVo
    ){
        TreeMap<String,String> map=new TreeMap<>();
        map.put("projectName",receiveVo.getProjectName());
        Signs.validResourceSign(receiveVo.getTimeStamp(),map,receiveVo.getSign(),null,signKey);
        List<ResourceReceive> resourceReceiveList= receiveVo.getResources();
        long areaTeamId=564701030634225664l;
        String departmentName="集团资源";
        resourceSyncService.receiveResFromRmp(areaTeamId,departmentName,resourceReceiveList);
        return ResponseResult.success("ok") ;
    }



}
